package com.ducquoc.examreviewdegreea1;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;
import android.widget.TabHost;

public class DocActivity extends Activity {

	private DocDatabase database = new DocDatabase(this);
	int version = 1;
	ArrayList<DocPojo> list = new ArrayList<DocPojo>();
	ListView lvTheory, lvSigns, lvCir;
	String currentDate = "", currentVersion = "", currentStatus = "null";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_doc);

		doFormWidgets();
		
		//Doc du lieu tu file test_database.txt nam trong thu muc ExamReviewDegreeA1
		//trong SdCard luu vao cac bien da khai bao
		DocReadWriteFile docRWFile = new DocReadWriteFile();
		ArrayList<String> arrTestDbs = docRWFile.ReadFile();

		//Gan du lieu co trong ArrayList vao cac bien
		currentDate = arrTestDbs.get(0).toString();
		currentVersion = arrTestDbs.get(1).toString();
		//version bien chinh dung de truy xuat du lieu trong SQLite
		version = Integer.parseInt(currentVersion);
		currentStatus = arrTestDbs.get(2).toString();

		// Load cac tab
		loadTabs();
	}

	// Cau hinh Tab
	public void loadTabs() {
		// Lay Tabhost id ra truoc (cai nay cua built - in android/
		final TabHost tab = (TabHost) findViewById(android.R.id.tabhost);
		// goi lenh setup
		tab.setup();
		TabHost.TabSpec spec;
		// Tao tabtheory
		spec = tab.newTabSpec("t1");
		spec.setContent(R.id.tabtheory);
		spec.setIndicator("Lý thuyết");
		tab.addTab(spec);
		// Tao tabsigns
		spec = tab.newTabSpec("t2");
		spec.setContent(R.id.tabsigns);
		spec.setIndicator("Biển báo");
		tab.addTab(spec);
		// Tao tabcircumstance
		spec = tab.newTabSpec("t3");
		spec.setContent(R.id.tabcircumstance);
		spec.setIndicator("Tình huống");
		tab.addTab(spec);
		// Thiet lap tab mac dinh duoc chon ban dau la tab 0
		tab.setCurrentTab(0);

		// Lay du lieu ban dau cho tab cau hoi Ly Thuyet neu CSDL da ton tai
		try {
			// Lay cau hoi tu bang tbl_theory
			database.open(version);
			// Xoa du lieu cu trong list
			list.clear();
			list = database.getDataTheory();
			database.close();

			setListTheory(list);
		} catch (Exception ex) {

		}

		// Su kien chon tab
		tab.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
			public void onTabChanged(String arg0) {

				if (tab.getCurrentTab() == 0) {
					// Lay cau hoi tu bang tbl_theory
					database.open(version);
					// Xoa du lieu cu trong list
					list.clear();
					list = database.getDataTheory();
					database.close();

					setListTheory(list);
				} else if (tab.getCurrentTab() == 1) {
					// Lay cau hoi tu bang tbl_sign
					database.open(version);
					// Xoa du lieu cu trong list
					list.clear();
					list = database.getDataSign();
					database.close();

					setListSigns(list);
				} else if (tab.getCurrentTab() == 2) {
					// Lay cau hoi tu bang tbl_sign
					database.open(version);
					// Xoa du lieu cu trong list
					list.clear();
					list = database.getDataCircumstance();
					database.close();

					setListCircumstance(list);
				}
			}
		});
	}

	// Dung so 1, 2 , 3 truyen qua DocArrayAdapter de xac dinh loai custom
	// listview
	// Dung custom listview de hien thi cau hoi trong ban tbl_theory.
	public void setListTheory(ArrayList<DocPojo> list) {
		this.list = list;

		DocArrayAdapter myArr = new DocArrayAdapter(this,
				R.layout.activity_doc_custom_listview_theory, list, 1);
		lvTheory.setAdapter(myArr);
	}

	// Dung custom listview de hien thi cau hoi trong ban tbl_sign.
	public void setListSigns(ArrayList<DocPojo> list) {
		this.list = list;

		DocArrayAdapter myArr = new DocArrayAdapter(this,
				R.layout.activity_doc_custom_listview_signs, list, 2);
		lvSigns.setAdapter(myArr);
	}

	// Dung custom listview de hien thi cau hoi trong ban tbl_circumstance.
	public void setListCircumstance(ArrayList<DocPojo> list) {
		this.list = list;

		DocArrayAdapter myArr = new DocArrayAdapter(this,
				R.layout.activity_doc_custom_listview_cir, list, 3);
		lvCir.setAdapter(myArr);
	}

	// Khoi tao cac doi tuong cho ListView
	public void doFormWidgets() {
		lvTheory = (ListView) findViewById(R.id.lvDocTabTheory);
		lvSigns = (ListView) findViewById(R.id.lvDocTabSigns);
		lvCir = (ListView) findViewById(R.id.lvDocTabCircumstance);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.doc, menu);
		return true;
	}
}
